<?php
require_once ('db.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);

error_reporting(E_ALL); // php errors
define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors

// path to 'MAIL.php' file from XPM4 package
require_once '../XPM4/MAIL.php';
require_once '../XPM4/POP3.php';

function getResponsesXpm4()
{
	$db = getDb();
	$sqlCommand="SELECT email,emailpasswd FROM access";
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	
	$myEmail = $result->fields[0];
	$myEmailPass = $result->fields[1];
	
	while(1) {
    
		// connect to POP3 server via SSL (SSL encryption) with authentication on port '995' and timeout '10' secounds
		// make sure you have OpenSSL module (extension) enable on your php configuration
		$c = POP3::connect('pop.gmail.com', $myEmail, $myEmailPass, 995, 'ssl', 10) or die(print_r($_RESULT));
		// STAT
		$s = POP3::pStat($c) or die(print_r($_RESULT));
		// $i - total number of messages, $b - total bytes
		list($i, $b) = each($s);
		if ($i > 0) { // if we have messages
			// RETR
			$r = POP3::pRetr($c, $i) or die(print_r($_RESULT)); // <- get the last mail (newest)
			// or pRetr($c, 1) <- get the old mail
			// print the source of message
			$message = explode("charset=UTF-8", $r);
			if(!isset($message[1]))
				{
					POP3::disconnect($c);
					return ;
				}
				
			$message = explode("2014-10", $message[1]);
			$message = $message[0];
			
			$tmp = explode("unsubscribe.php?", $r);
			$tmp = explode("=", $tmp[1]);
			$subeventid = $tmp[3];
			
			$tmp = explode("&", $tmp[1]);
			$eventId = $tmp[0];
			$tmp = explode("&", $tmp[2]);
			$emailId = $tmp[0];
			
			$sqlCommand="INSERT INTO response(emailid,subeventid, data) values(".
					$emailId.",".
					$subeventid . ",'".
					$message . "')";
			//echo ($sqlCommand);
			$result = $db->Execute($sqlCommand);
			
			//if ($result === false)
			//	echo("SQL ERROR: " . $sqlCommand);
			
			/*
			echo($message);
			echo($emailId . " ");
			echo($subeventid . " ");
			echo($eventId . " ");
			print_r($r);*/
			
			
			/*
			echo $message;*/
			// optional, you can delete this message from server
			//POP3::pDele($c, $i) or die(print_r($_RESULT));
		} else break;
		// disconnect
		POP3::disconnect($c);
	}
}


function sendEmailXpm4($address,$cc,$bcc,$subject,$message,$attachmentPath,$attachmentName)
{
	
	$db = getDb();
	$sqlCommand="SELECT email,emailpasswd FROM access";
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	
	$myEmail = $result->fields[0];
	$myEmailPass = $result->fields[1];
	
	
	// initialize MAIL class
	$m = new MAIL;
	// set from address
	$m->From($myEmail);
	// add to address
	$m->AddTo($address);
	// add cc addresses
	foreach ($cc as $ccAddress) {
		$m->AddCc($ccAddress);
	}
	// add bcc addresses
	foreach ($bcc as $bccAddress) {
		$m->AddBcc($bccAddress);
	}

	$fileContent = @file_get_contents($attachmentPath);
	if ($fileContent === FALSE) {
	} else {
		
		// attachment
		// attach file '$f', disposition 'inline' and give a name 'photo.gif' with ID value (this ID value can be used in embed HTML images)
		$a = $m->Attach($fileContent, FUNC::mime_type($attachmentPath), $attachmentName, null, null, 'inline', MIME::unique());
		
		
	}
	
	
	// set subject
	$m->Subject($subject);
	// set text message
	$m->Text($message);
	
	// connect to MTA server 'smtp.gmail.com' port '465' via SSL ('tls' encryption) with authentication: 'username@gmail.com'/'password'
	// set the connection timeout to 10 seconds, the name of your host 'localhost' and the authentication method to 'plain'
	// make sure you have OpenSSL module (extension) enable on your php configuration
	$c = $m->Connect('smtp.gmail.com', 465, $myEmail, $myEmailPass, 'tls', 10, 'localhost', null, 'plain') or die(print_r($m->Result));
	
	// send mail relay using the '$c' resource connection
	echo $m->Send($c) ? 'Mail sent !' : 'Error !';
	
	// disconnect from server
	$m->Disconnect();
	/*
	// optional for debugging ----------------
	echo '<br /><pre>';
	// print History
	print_r($m->History);
	// calculate time
	list($tm1, $ar1) = each($m->History[0]);
	list($tm2, $ar2) = each($m->History[count($m->History)-1]);
	echo 'The process took: '.(floatval($tm2)-floatval($tm1)).' seconds.</pre>';
	*/
}

?>
